﻿using System.Collections.Generic;
using System.Runtime.Serialization;
using UnityEngine;

namespace GMME
{
    [DataContract]
    public class Room
    {
        // 房间ID
        [DataMember(Name = "roomId")] public string RoomId { get; set; }

        // 房间类型 1：小队语音，2：国战语音
        [DataMember(Name = "roomType")] public int RoomType { get; set; }

        // 房主openID
        [DataMember(Name = "ownerId")] public string OwnerId { get; set; }

        // 玩家列表
        [DataMember(Name = "players")] public List<Player> Players { get; set; }

        // 房间状态 0-房间正常 1-房间禁言
        [DataMember(Name = "roomStatus")] public int RoomStatus { get; set; }

#if UNITY_ANDROID
        internal static Room ConvertRoom(AndroidJavaObject javaObject)
        {
            Room room = new Room();
            if (javaObject != null)
            {
                string roomId = javaObject.Call<string>("getRoomId");
                room.RoomId = roomId;
                string ownerId = javaObject.Call<string>("getOwnerId");
                room.OwnerId = ownerId;
                int roomType = javaObject.Call<int>("getRoomType");
                room.RoomType = roomType;
                int status = javaObject.Call<int>("getRoomStatus");
                room.RoomStatus = status;
                room.Players = new List<Player>();
                AndroidJavaObject playersObject = javaObject.Call<AndroidJavaObject>("getPlayers");
                if (playersObject != null)
                {
                    AndroidJavaObject[] players = playersObject.Call<AndroidJavaObject[]>("toArray");
                    foreach (AndroidJavaObject playerObject in players)
                    {
                        Player player = new Player();
                        player.OpenId = playerObject.Call<string>("getOpenId");
                        player.RoleType = playerObject.Call<int>("getRoleType");
                        player.IsForbidden = playerObject.Call<int>("getIsForbidden");
                        room.Players.Add(player);
                    }
                }

                Debug.LogFormat("RoomId={0}, OwnerId={1}, Players Size={2}", room.RoomId, room.OwnerId,
                    room.Players.Count);
            }

            return room;
        }
#endif
    }

    [DataContract]
    public class Player
    {
        // 玩家ID
        [DataMember(Name = "openId")] public string OpenId { get; set; }

        // 国战语音的玩家角色 1：指挥官， 2：普通听众
        [DataMember(Name = "roleType")] public int RoleType { get; set; }

        // 玩家是否被禁言 0-未被禁言 1-被禁言
        [DataMember(Name = "isForbidden")] public int IsForbidden { get; set; }
    }

    // 玩家的音量大小
    [DataContract]
    public class VolumeInfo
    {
        [DataMember(Name = "openId")] public string OpenId { get; set; }

        // 音量大小0-100
        [DataMember(Name = "volume")] public int Volume { get; set; }
    }
}